package com.itbour.service.user.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.dao.user.UserInfoDao;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.user.GetOssFileListUtil;
import com.itbour.dto.user.UserStatsDto;
import com.itbour.service.user.UserInfoService;

public class UserInfoServiceImpl implements UserInfoService {

	private UserInfoDao userInfoDao;
	
	public Page getUserInfo(Map<String, Object> parm, Integer pageNum,
			Integer pageSize) {
		Page page = null;
		int skipNum = JspPage.getStartOfPage(pageNum, pageSize);// 获取起始位置
		int totalSize = userInfoDao.getUserInfoCnt(parm);
		while (totalSize > 0 && totalSize <= skipNum) {
			skipNum = skipNum - pageSize;
			pageNum = pageNum - 1;
		}
		parm.put("pageSize", pageSize);
		parm.put("skipResults", skipNum);
		List<UserInfoDto> data = userInfoDao.getUserInfo(parm);
		page = new JspPage(skipNum, totalSize, pageSize, data);
		return page;
	}
	
	// 获取阿里oss文件夹列表
	public Page getOssFileList(Map<String, Object> parm, Integer pageNum,Integer pageSize) {
		Page page = null;
		String nextMarker= (String)parm.get("nextMarker");
		String prefix= (String)parm.get("prefix");
		
		if (prefix == null){
			prefix = "";
		}
		
		List<OSSObjectSummary> sums = new ArrayList<OSSObjectSummary>();
		ObjectListing obj = null;
		obj = GetOssFileListUtil.pageGetAllByPrefix(nextMarker, prefix);
		sums = obj.getObjectSummaries();
		
		
		System.out.println(obj.getCommonPrefixes());// 当前文件夹下的文件夹
		for (String commonPrefix : obj.getCommonPrefixes()) {
			OSSObjectSummary o = new OSSObjectSummary();
			o.setKey(subSt(prefix,commonPrefix));
			sums.add(o);
		}
		if (sums.size()>=1){
			sums.remove(0);
		}
		
		for (OSSObjectSummary s : sums) {
			System.out.println("\t" + s.getKey());
		}
		
		
		
		int skipNum = JspPage.getStartOfPage(pageNum, pageSize);// 获取起始位置
		int totalSize = sums.size();// 总的个数
		
		page = new JspPage(skipNum, totalSize, 10, sums);
		return page;
	}
	
	public static String subSt(String pre,String comf){
		if (pre!=null) {
			int len = pre.length();
			return comf.substring(len);
		}
		return comf;
	}

	//修改用户信息
	public void updUserInfo(UserInfoDto u) {
		Map<String, Object> parm =new HashMap<String, Object>();
		if(u.getUser_id()!=null){
			if(u.getUser_name()!=null){
				parm.put("user_name",u.getUser_name());
			}
			if(u.getNickname()!=null){
				parm.put("nickname",u.getNickname());
			}
			if(u.getUser_type()!=null){
				parm.put("user_type",u.getUser_type());
			}
			parm.put("user_id",u.getUser_id());
			parm.put("update_by",u.getUpdate_by());
			userInfoDao.updUserInfo(parm);
		}
	}

	//获取单个用户信息
	public List<UserInfoDto> getOneUserInfo(Map<String, Object> parm) {
		return userInfoDao.getOneUserInfo(parm);
	}

	
	
	
	public UserInfoDao getUserInfoDao() {
		return userInfoDao;
	}
	public void setUserInfoDao(UserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}

	
	/**
	 * *********************** 用户统计
	 */
	public Page getUserStats(Map<String, Object> parm, Integer pageNum,
			Integer pageSize) {
		Page page = null;
		int skipNum = JspPage.getStartOfPage(pageNum, pageSize);// 获取起始位置
		int totalSize = userInfoDao.getUserStatsCnt(parm);
		while (totalSize > 0 && totalSize <= skipNum) {
			skipNum = skipNum - pageSize;
			pageNum = pageNum - 1;
		}
		parm.put("pageSize", pageSize);
		parm.put("skipResults", skipNum);
		List<UserStatsDto> data = userInfoDao.getUserStats(parm);
		page = new JspPage(skipNum, totalSize, pageSize, data);
		return page;
	}
}
